package com.hongdee.scm.approvalflow.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.approvalflow.entity.TScmApprovalflowDetail;
import com.hongdee.scm.approvalflow.service.ITScmApprovalflowDetailService;
import com.hongdee.scm.pushMessage.entity.TScmMapToJSON;
import com.hongdee.scm.pushMessage.service.ITScmPushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmApprovalflowDetail
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.approvalflow.controller
 * 作  者: 术洪超
 * 日  期:2017-06-20 17:08
 */
@RestController
@RequestMapping("/scm/approvalflowDetail")
public class TScmApprovalflowDetailController {

    @Autowired
    private ITScmApprovalflowDetailService itScmApprovalflowDetailService;

    @Autowired
    private ITScmPushMsgService itScmPushMsgService;


    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "approvalflowPId", defaultValue = "") String approvalflowPId,
                             @RequestBody TScmApprovalflowDetail data) throws Exception {
        Map map = this.itScmApprovalflowDetailService.save(data, approvalflowPId, uid);
        if(map.get("type").equals(1)){
            List saId = (ArrayList) map.get("saId");
            map.remove("saId");
            String title= TScmMapToJSON.obj2JsonString(map);
            //个推(返回审批明细)
            itScmPushMsgService.push(saId,title,"1");
        }
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

     /**
          * 审批明细分页
          * @param uid
          * @param token
          * @param start
          * @param limit
          * @param data
          * @return
          * @throws Exception
          */
         @RequestMapping("/page")
         @ResponseBody
         public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "start", defaultValue = "0") String start,
                         @RequestParam(name = "limit", defaultValue = "50") String limit,
                         @RequestBody Map data) throws Exception {

             Map map = this.itScmApprovalflowDetailService.page(data, start, limit, uid);
             map.put("success", true);
             map.put("msg", "分页查询成功！");
             return map;
         }

}
